# Based on scanpy paga tutorial
# %%
import numpy as np
import pandas as pd
import matplotlib.pyplot as pl
from matplotlib import rcParams
import scanpy as sc
# verbosity: errors (0), warnings (1), info (2), hints (3). default is warning (1)
sc.settings.verbosity = 2
adata = sc.datasets.paul15()
adata

# %%
# set higher precision to expr matrix
adata.X = adata.X.astype('float64')  # this is not required and results will be comparable without it

# %%
# use built-in recipe (normalize - find high var)
sc.pp.recipe_zheng17(adata)

# %%
# infer trajectory
sc.pp.pca(adata)
sc.pp.neighbors(adata, n_neighbors=4, n_pcs=20)
# force-directed graph drawing, alternative to tSNE
# default optimized method need install fa2
sc.tl.draw_graph(adata)
sc.pl.draw_graph(adata, color='paul15_clusters', legend_loc='on data')

# %%
# optional: denoising the graph in diffusion map
sc.tl.diffmap(adata)
sc.pp.neighbors(adata, n_neighbors=10, use_rep='X_diffmap')
sc.tl.draw_graph(adata)
sc.pl.draw_graph(adata, color='paul15_clusters', legend_loc='on data')

# %%
# clustering and PAGA
sc.tl.louvain(adata)
sc.tl.paga(adata, groups='louvain')
# also plot gene expr in nodes
sc.pl.paga(adata, color=['louvain', 'Hba-a2', 'Elane', 'Irf8'])

# %%
# annotate clusters
adata.obs['louvain_anno'] = adata.obs['louvain']
# set category index
adata.obs['louvain_anno'].cat.categories = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10/Ery', '11', '12',
       '13', '14', '15', '16/Stem', '17', '18', '19/Neu', '20/Mk', '21', '22/Baso', '23', '24/Mo']
sc.tl.paga(adata, groups='louvain_anno')
sc.pl.paga(adata, threshold=0.03, show=False)
# %%
